/**
 * project2
 *
 * brief description of the program
 *
 * This program is released to public domain according to
 * author's wish, you can view full license at
 * http://tomatoeskit.org/upload/PublicDomainLicense.txt
 *
 * @author Fangning CHENG (Alpha.L Cheng) <chengf@purdue.edu>
 *
 * @lab section number 
 *
 * @date Sep 16, 2013
 *
 */

public class LineSegment {
 
    /**
     * The start and end Points of this LineSegment.
     */
    private final Point start;
    private final Point end;
 
    /**
     * Construct a LineSegment having the given Point parameters as start and end.
     * 
     * @param start the Point (X1, Y1) at the start of this LineSegment.
     * @param end the Point (X2, Y2) at the end of this LineSegment.
     */
    public LineSegment(Point start, Point end) {
        //initialize instance variables here
        this.start = start;
        this.end = end;
    }
 
 
    /**
     * @return the slope of this LineSegment, calculated using the formula: 
     *         (Y2 - Y1)/(X2 - X1). 
     */
    public double slope() {
        return ((double)end.getY() - start.getY()) / (end.getX() - start.getX());
    }
 
 
 
    /**
     * @return the Y intercept of this LineSegment, calculated using the formula:
     *         Y1 - (M * X1)
     *         where M is the slope of this LineSegment.
     */
    public double yIntercept() {
        return start.getY() - (slope() * start.getX());
    }
 
 
    /**
     * @return the start Point of this LineSegment. 
     *         This corresponds to the Point (X1, Y1).
     */
    public Point getStart() {
        return start;
    }
 
 
    /**
     * @return the end Point of this LineSegment.
     *         This corresponds to the Point (X2, Y2).
     */
    public Point getEnd() {
        return end;
    }
 
 
    /**
     * @return the length of this LineSegment, calculated using the formula: 
     *         sqrt((X2 - X1)^2 + (Y2 - Y1)^2).
     */
    public double length() {
        return Math.sqrt((end.getX() - start.getX()) * (end.getX() - start.getX())
                + (end.getY() - start.getY()) * (end.getY() - start.getY()));
    }
 
 
    /**
     * @param scalar the double value to be used to
     *        scale this LineSegment. Scaling is done 
     *        with the following formula:
     *        (fill in the formula you will use here!).
     * @return a new LineSegment whose start Point is the
     *         same start Point of this LineSegment and
     *         the end Point has been scaled by the given
     *         scalar value.        
     */
    public LineSegment scaleByFactor(double scalar) {
        double newXLength = (end.getX() - start.getX()) * scalar;
        double newYLength = (end.getY() - start.getY()) * scalar;
        return new LineSegment(start, new Point(start.getX() + newXLength, start.getY() + newYLength));
    }
 
 
    /**
     * @return the following String message: 
     *         "y = M x + B" 
     *         where M is the slope of this LineSegment, 
     *         and B is the Y intercept of this LineSegment. 
     *         For example: if the slope were 5 and the Y
     *         intercept were 7, this method would return:
     *         "y = 5 x + 7".
     */
    @Override
    public String toString() {
        return String.format("y = %.2f x + %.2f", slope(), yIntercept());
    }
    
    public static String fmt(double d)
    {
        if (d == (int) d)
            return String.format("%d", (int)d);
        else
            return String.format("%s", d);
    }
}